package com.jianzhile.jobs.fragments;

import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.jianzhile.jobs.R;
import com.jianzhile.jobs.adapter.ConstellationAdapter;
import com.jianzhile.jobs.adapter.GirdDropDownAdapter;
import com.jianzhile.jobs.adapter.ListDropDownAdapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.alide.dropdownmenu.DropDownMenu;

/**
 * Create by terry on 16/3/4
 * 兼职页
 */
public class WorkFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;


    private OnFragmentInteractionListener mListener;
    @Bind(R.id.dropDownMenu)
    public DropDownMenu mDropDownMenu;

    private String headers[] = {"位置", "时间", "岗位", "其他"};
    private List<View> popupViews = new ArrayList<>();

    private GirdDropDownAdapter cityAdapter;
    private ListDropDownAdapter ageAdapter;
    private ListDropDownAdapter sexAdapter;
    private ConstellationAdapter constellationAdapter;


    private String citys[] = {"不限", "武汉", "北京", "上海", "成都", "广州", "深圳", "重庆", "天津", "西安", "南京", "杭州"};
    private String ages[] = {"不限", "今天", "明天", "周末", "未来一周内", "长期"};
    private String sexs[] = {"猜你喜欢", "高薪直聘", "企业直招", "附近兼职"};
    private String constellations[] = {"不限", "观众群演", "场务礼仪", "销售地推", "家教", "话务员", "翻译设计", "网络", "实习", "其他"};
    private int consicons[] = {R.drawable.ic_access_time_grey_400_18dp, R.drawable.ic_people_red_400_48dp, R.drawable.ic_local_activity_red_400_48dp, R.drawable.ic_people_red_400_48dp, R.drawable.ic_people_red_400_48dp, R.drawable.ic_people_red_400_48dp, R.drawable.ic_people_red_400_48dp, R.drawable.ic_people_red_400_48dp, R.drawable.ic_people_red_400_48dp};
    private int constellationPosition = 0;

    public WorkFragment() {
    }


    public static WorkFragment newInstance(String param1, String param2) {
        WorkFragment fragment = new WorkFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_work, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this, getActivity());

        final ListView cityView = new ListView(getActivity());
        cityAdapter = new GirdDropDownAdapter(getActivity(), Arrays.asList(citys));
        cityView.setDividerHeight(0);
        cityView.setAdapter(cityAdapter);

        final ListView ageView = new ListView(getActivity());
        ageView.setDividerHeight(0);
        ageAdapter = new ListDropDownAdapter(getActivity(), Arrays.asList(ages));
        ageView.setAdapter(ageAdapter);

        final ListView sexView = new ListView(getActivity());
        sexView.setDividerHeight(0);
        sexAdapter = new ListDropDownAdapter(getActivity(), Arrays.asList(sexs));
        sexView.setAdapter(sexAdapter);

        final View constellationView = getActivity().getLayoutInflater().inflate(R.layout.custom_layout, null);
        GridView constellation = ButterKnife.findById(constellationView, R.id.constellation);
        constellationAdapter = new ConstellationAdapter(getActivity(), Arrays.asList(constellations));
        constellation.setAdapter(constellationAdapter);
        TextView ok = ButterKnife.findById(constellationView, R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDropDownMenu.setTabText(constellationPosition == 0 ? headers[3] : constellations[constellationPosition]);
                mDropDownMenu.closeMenu();
            }
        });

        //init popupViews
        popupViews.add(cityView);
        popupViews.add(ageView);
        popupViews.add(constellationView);
        popupViews.add(sexView);

        cityView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[0] : citys[position]);
                mDropDownMenu.closeMenu();
            }
        });

        ageView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ageAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[1] : ages[position]);
                mDropDownMenu.closeMenu();
            }
        });

        sexView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sexAdapter.setCheckItem(position);
                mDropDownMenu.setTabText(position == 0 ? headers[2] : sexs[position]);
                mDropDownMenu.closeMenu();
            }
        });

        constellation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                constellationAdapter.setCheckItem(position);
                constellationPosition = position;
            }
        });

        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.work_fragment_content, null);
        contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        ListView lv = (ListView) contentView.findViewById(R.id.work_fragment_content_lv);
        lv.setDividerHeight(0);
//        LinearLayout ll = new LinearLayout(getActivity());
//        ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, CommonUtil.dip2px(getActivity(), 20)));
//        lv.addHeaderView(ll);
        lv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return 10;
            }

            @Override
            public Object getItem(int position) {
                return position;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(getActivity()).inflate(R.layout.job_item_3, null);
                    new ViewHolder(convertView);
                }
                ViewHolder viewHolder = (ViewHolder) convertView.getTag();
                return convertView;
            }

            class ViewHolder {

                public ViewHolder(View view) {
                    view.setTag(this);
                }
            }
        });

        mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);

//        if (keyWordType != -1 && sexAdapter != null){
//            sexAdapter.setCheckItem(keyWordType);
//            mDropDownMenu.switchMenu(sexView);
//            //((TextView)mDropDownMenu.getTabMenuView().getChildAt(3)).setText("aaa");
//            //Snackbar.make(sexView, "set", Snackbar.LENGTH_LONG).show();
//          //  mDropDownMenu.closeMenu();
 //       }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;

        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }


    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);

        void initToolBar(int tab);
    }


    public static int keyWordType = -1;

    public void chooseByKeyWord(int type) {
        switch (type) {
            case 0:
                keyWordType = 1;
                break;
            case 1:
                keyWordType = 2;
                break;
            case 2:
                keyWordType = 3;
                break;
            case 3:
                keyWordType = 4;
                break;
        }

    }


}
